package com.nirvana.utils.http;

import javax.servlet.http.HttpServletRequest;

/**
 * 
 * @author yshqiu
 */
public class HttpUtils {
	
	private final static String XML_HTTP_REQUEST_HEAD = "x-requested-with";
	private final static String XML_HTTP_REQUEST = "XMLHttpRequest";
	private final static String JSON_CONTENT_TYPE = "application/json";
	private final static String XML_CONTENT_TYPE = "text/xml";

	// 是否ajax请求
	public static boolean isAjaxRequest(HttpServletRequest request) {
		
		if (XML_HTTP_REQUEST.equalsIgnoreCase(request.getHeader(XML_HTTP_REQUEST_HEAD))) {
			return true;
		}
		return false;
	}

	// 是否是json请求
	public static boolean isJsonRequest(HttpServletRequest request) {
		String accept = request.getHeader("accept");
		if (isAjaxRequest(request) && accept != null) {
			// if(accept.indexOf(JSON_CONTENT_TYPE) >= 0)
			return true;
		}
		return false;
	}

	// contentType是否是xml格式的
	public static boolean isXMLRequest(HttpServletRequest request) {
		String contentType = request.getContentType();
		if (isAjaxRequest(request) && contentType != null) {
			if (contentType.indexOf(XML_CONTENT_TYPE) >= 0)
				return true;
		}
		return false;
	}

}
